package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class GuannewsNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.guannews.com/.*/[0-9]+/[0-9]+/[0-9]+.html",
				"http://www.guannews.com/.*/[0-9]*.html"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);
		doc.select(".relateNews").remove();
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("h1"));
		title=tool.parse();


		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("div.tenttb"));
		date=tool.parseDate();
		

		/*
		 * 解析内容
		 */
		tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("div.ccont"));
		content=tool.parse();

		/*
		 * 解析作者
		 */
		author= JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}
		
		 {
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);
		
		return meta;
	
		
	}
	public static void main(String[] args) {
		GuannewsNewsAnalyse hsw=new GuannewsNewsAnalyse();
		String url="http://www.guannews.com/xinwen/zhufang/2014/0514/16469.html";
		System.out.println(hsw.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=hsw.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	
	public boolean isNeedUpdate(){
		return false;
	}

}
